package cn.jimmie.learn.viewArt.captcha.toolbox;

import android.graphics.*;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.widget.ImageView;
import cn.jimmie.learn.viewArt.captcha.internal.BlockFunc;
import cn.jimmie.learn.viewArt.captcha.internal.Position;

import java.util.Random;

/**
 * function :
 *
 * <p> Create Time: 2018年12月09日 11:18 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public class PuzzleBlockFunc implements BlockFunc {
    // 随机因子, 为了生成, 不同规则的 拼图图片
    private static final int SEED = 1 << 12;
    // 随机器
    private Random mRandom = new Random();

    // 以下变量, 在重新绘制 mPath 和 bitmap 时,会重复创建, 因此使用成员变量
    private Paint mBlockPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
    private PorterDuffXfermode mPorterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
    private PaintFlagsDrawFilter mFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

    public PuzzleBlockFunc() {
    }

    @Override
    public void generateBlockPath(Path path, Position position, int blockWidth, int blockHeight) {
        // 清空路径
        path.reset();

        // 生成拼图 块
        BezierUtils.drawPuzzle(path, position.x, position.y, blockWidth / 3, mRandom.nextInt(SEED));
    }

    @Override
    public void generateRandomPosition(Position position, int blockWidth, int blockHeight, int viewWidth, int viewHeight) {
        position.x = mRandom.nextInt(viewWidth - blockWidth * 3) + blockWidth;
        position.y = mRandom.nextInt(viewHeight - blockHeight * 3) + blockHeight;
    }

    @Override
    public Bitmap createBlockBitmap(ImageView view, Path path) {
        // 拿到背景图片
        Bitmap source;
        Drawable drawable = view.getDrawable();
        if (drawable instanceof RoundedBitmapDrawable) {
            source = ((RoundedBitmapDrawable) drawable).getBitmap();
        } else {
            source = ((BitmapDrawable) drawable).getBitmap();
        }

        // 以控件宽高 create一块bitmap
        Bitmap block = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        // 把创建的bitmap作为画板
        Canvas canvas = new Canvas(block);
        // 抗锯齿
        canvas.setDrawFilter(mFilter);
        // 绘制 缺块 区域
        canvas.drawPath(path, mBlockPaint);
        // 设置遮罩模式 只显示 缺块上的图片
        mBlockPaint.setXfermode(mPorterDuffXfermode);

        // 考虑到scaleType等因素，要用Matrix对Bitmap进行缩放
        //noinspection ConstantConditions
        canvas.drawBitmap(source, view.getImageMatrix(), mBlockPaint);

        mBlockPaint.setStyle(Paint.Style.STROKE);
        mBlockPaint.setColor(Color.WHITE);
        mBlockPaint.setStrokeWidth(10);
        canvas.drawPath(path, mBlockPaint);

        mBlockPaint.reset();

        return block;
    }

    @Override
    public Paint createBlockShadowPaint() {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        // 设置 画笔的阴影效果
        paint.setMaskFilter(new BlurMaskFilter(15, BlurMaskFilter.Blur.SOLID));
//        paint.setColor(Color.WHITE);
        paint.setAlpha(150);
        return paint;
    }

    @Override
    public Paint createBlockMaskPaint() {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        paint.setMaskFilter(new BlurMaskFilter(15, BlurMaskFilter.Blur.SOLID));
        paint.setColor(Color.BLACK);
        return paint;
    }
}
